package cdc.retrofit.demo;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;

import cdc.tools.FileResult;
import cdc.tools.User;
import cdc.tools.UserTools;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class UserRequest {
	public static final String API_URL = "http://192.168.0.164:8080/api/";
	private Retrofit retrofit;
	private Api api;

	public UserRequest() {
		retrofit = new Retrofit.Builder().baseUrl(API_URL).addConverterFactory(GsonConverterFactory.create()).build();
		api = retrofit.create(Api.class);
	}

	public void getUsers() {
		Call<List<User>> call = api.listUsers();
		call.enqueue(new Callback<List<User>>() {

			@Override
			public void onResponse(Call<List<User>> call, Response<List<User>> response) {
				// TODO Auto-generated method stub
				request(call);
				List<User> lists = response.body();
				for (User user : lists) {
					System.out.println(user.getName() + "==" + user.getId());
				}
			}

			@Override
			public void onFailure(Call<List<User>> call, Throwable t) {
				// TODO Auto-generated method stub

			}
		});
	}

	public void userid() {
		Call<User> call = api.userid(1);
		call.enqueue(new Callback<User>() {

			@Override
			public void onResponse(Call<User> call, Response<User> response) {
				// TODO Auto-generated method stub
				request(call);
				User user = response.body();
				System.out.println(user.getName());

			}

			@Override
			public void onFailure(Call<User> call, Throwable t) {
				// TODO Auto-generated method stub

			}
		});

	}

	public void iduser() {
		Call<User> call = api.iduser(2);
		call.enqueue(new Callback<User>() {

			@Override
			public void onResponse(Call<User> call, Response<User> response) {
				// TODO Auto-generated method stub
				request(call);
				User user = response.body();
				System.out.println(user.getName());
			}

			@Override
			public void onFailure(Call<User> call, Throwable t) {
				// TODO Auto-generated method stub

			}
		});
	}

	public void user() {
		Call<User> call = api.user(3);
		call.enqueue(new Callback<User>() {

			@Override
			public void onResponse(Call<User> call, Response<User> response) {
				// TODO Auto-generated method stub
				request(call);
				User user = response.body();
				System.out.println(user.getName());
			}

			@Override
			public void onFailure(Call<User> call, Throwable t) {
				// TODO Auto-generated method stub

			}
		});

	}

	/**
	 * get 上传两个参数
	 */
	public void userBoth() {
		Call<User> call = api.userboth(3, "陈");
		call.enqueue(new Callback<User>() {

			@Override
			public void onResponse(Call<User> call, Response<User> response) {
				// TODO Auto-generated method stub
				request(call);
				User user = response.body();
				System.out.println(user.getName());
			}

			@Override
			public void onFailure(Call<User> call, Throwable t) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * get post Map方式上传两个参数
	 */
	public void userBothMap() {
		Map<String, Object> map = new HashMap<>();
		map.put("id", 1);
		map.put("name", "颚桂");
		Call<User> call = api.userbothMap(map);
		call.enqueue(new Callback<User>() {

			@Override
			public void onResponse(Call<User> call, Response<User> response) {
				// TODO Auto-generated method stub
				request(call);
				User user = response.body();
				System.out.println(user.getName());
			}

			@Override
			public void onFailure(Call<User> call, Throwable t) {
				// TODO Auto-generated method stub

			}
		});
	}
	/**
	 * 以表单的方式上传  两个参数
	 */
	public void userBothForm() {
		Call<User> call = api.userbothForm(3, "李侍尧");
		call.enqueue(new Callback<User>() {

			@Override
			public void onResponse(Call<User> call, Response<User> response) {
				// TODO Auto-generated method stub
				request(call);
				User user = response.body();
				System.out.println(user.getName());
			}

			@Override
			public void onFailure(Call<User> call, Throwable t) {
				// TODO Auto-generated method stub
			}
		});
	}
	
	/**
	 * 上传json数据，并返回json数据
	 */
	public void upUser() {
		User user = UserTools.initDefaultUser();
		Gson gson = new Gson();
		String route = gson.toJson(user);// 通过Gson将Bean转化为Json字符串形式
		System.out.println(route);
		RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), route);
		Call<User> call = api.createUser(body);
		call.enqueue(new Callback<User>() {

			@Override
			public void onResponse(Call<User> call, Response<User> response) {
				// TODO Auto-generated method stub
				request(call);
				System.out.println(response.code());
				User user = response.body();
				System.out.println(user.getName());
			}

			@Override
			public void onFailure(Call<User> call, Throwable t) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 上传一个文件 和两个参数
	 * @param file  用@Part MultipartBody.Part 注解来定义我们要上传的图片文件
	 * @param id    用@Part("attribute") RequestBody 直接来定义请求中的字符串字段
	 * @param name  用@Part("attribute") RequestBody 直接来定义请求中的字符串字段
	 * @return
	 */
	public void upload() {
		// 创建RequestBody，传入参数："multipart/form-data"，String
		RequestBody requestIdKey = RequestBody.create(MediaType.parse("multipart/form-data"), 12 + "");
		RequestBody requestNameKey = RequestBody.create(MediaType.parse("multipart/form-data"), "和珅");
		User user = UserTools.initDefaultUser();
		Gson gson = new Gson();
		String route = gson.toJson(user);// 通过Gson将Bean转化为Json字符串形式
		System.out.println(route);
		File file = new File("H:\\图片\\1.png");
		RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
		MultipartBody.Part bodyFile = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
		Call<FileResult> call = api.updateUser(bodyFile, requestIdKey, requestNameKey);
		call.enqueue(new Callback<FileResult>() {

			@Override
			public void onResponse(Call<FileResult> call, Response<FileResult> response) {
				// TODO Auto-generated method stub
				request(call);
				System.out.println(response.code());
				System.out.println(response.body().toString());
			}

			@Override
			public void onFailure(Call<FileResult> call, Throwable t) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 上传一个文件 和两个参数
	 * @param RequestBody fileParamers
	 * @return
	 */
	public void upload2() {
		File file = new File("H:\\图片\\1.png");
		RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
		RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("id", "898")
				.addFormDataPart("name", "福长安").addFormDataPart("file", file.getName(), requestFile).build();
		Call<FileResult> call = api.updateUser2(requestBody);
		call.enqueue(new Callback<FileResult>() {

			@Override
			public void onResponse(Call<FileResult> call, Response<FileResult> response) {
				// TODO Auto-generated method stub
				request(call);
				System.out.println(response.code());
				System.out.println(response.body().toString());
			}

			@Override
			public void onFailure(Call<FileResult> call, Throwable t) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 上传多个文件 和两个参数2
	 */
	public void upload3() {
		File file1 = new File("H:\\图片\\1.png");
		File file2 = new File("H:\\图片\\2.png");
		File file3 = new File("H:\\图片\\3.png");
		RequestBody requestFile1 = RequestBody.create(MediaType.parse("multipart/form-data"), file1);
		RequestBody requestFile2 = RequestBody.create(MediaType.parse("multipart/form-data"), file2);
		RequestBody requestFile3 = RequestBody.create(MediaType.parse("multipart/form-data"), file3);
		RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("id", "898")
				.addFormDataPart("name", "福长安").addFormDataPart("file", file1.getName(), requestFile1)
				.addFormDataPart("file", file2.getName(), requestFile2)
				.addFormDataPart("file", file3.getName(), requestFile3).build();
		Call<FileResult> call = api.updateUser3(requestBody);
		call.enqueue(new Callback<FileResult>() {

			@Override
			public void onResponse(Call<FileResult> call, Response<FileResult> response) {
				// TODO Auto-generated method stub
				request(call);
				System.out.println(response.code());
				FileResult fileResult = response.body();
				System.out.println(fileResult.toString());
			}

			@Override
			public void onFailure(Call<FileResult> call, Throwable t) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void request(Call<?> call) {
		Request request = call.request();
		headers(request);
		urlMethod(request);
	}

	private void urlMethod(Request request) {
		String method = request.method();
		String url = request.url().url().toString();
		System.out.println("method=" + method + ",url=" + url);
	}

	/**
	 * 打印请求头
	 * 
	 * @param request
	 */
	private void headers(Request request) {
		Headers headers = request.headers();
		int size = headers.size();
		System.out.println(size + "," + headers.toString());
		System.out.println("========header======================");
		for (int i = 0; i < size; i++) {
			String name = headers.name(i);
			String value = headers.value(i);
			System.out.println("header_name=" + name + ",header_value=" + value);
		}
		System.out.println("========header======================");
	}

}
